package com.tokenplus.thai.components.dialog

import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.tokenplus.thai.R
import com.tokenplus.thai.databinding.DialogCheckinRemindBinding

class CheckinRemindDialog : DialogFragment() {

    //布局
    lateinit var mViewBinding: DialogCheckinRemindBinding

    //点击确认监听
    private var mOnConfirmListener: () -> Unit = { }

    private var mIncome: String? = null

    companion object {
        /** 显示弹框 */
        fun showDialog(fragmentManager: FragmentManager) {
            val dialog = CheckinRemindDialog()
            dialog.showNow(fragmentManager, CheckinRemindDialog::class.java.simpleName)
        }

        /** 显示弹框 */
        fun showDialog(fragmentManager: FragmentManager, income: String, callback: () -> Unit) {
            val dialog = CheckinRemindDialog()
            dialog.mIncome = income
            dialog.setConfirmClickListener(callback)
            dialog.showNow(fragmentManager, CheckinRemindDialog::class.java.simpleName)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        mViewBinding =
            DialogCheckinRemindBinding.inflate(layoutInflater)
        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initSubViews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }

        mViewBinding.checkin.text = getString(R.string.check_in2, mIncome ?: "")
    }

    private fun initSubViews() {
        mViewBinding.checkin.setOnClickListener {
            mOnConfirmListener.invoke()
            dismiss()
        }
    }

    /** 设置点击事件 */
    fun setConfirmClickListener(listener: () -> Unit) {
        mOnConfirmListener = listener
    }
}